﻿#include "self_mode.h"
#include "common.h"
#include <list>


#define SELF_WATER_MODE_JSON_PATH "/data/selfwater.json"

#define WATER_INFO_EX_MODE "mode"
#define WATER_INFO_EX_NAME "name"
#define WATER_INFO_EX_ICON "icon"
#define WATER_INFO_EX_COLLECTED_MODE "collected_mode"
#define WATER_INFO_EX_WATER_OUTPUT_MIN "water_output_min"
#define WATER_INFO_EX_WATER_OUTPUT_MAX "water_output_max"
#define WATER_INFO_EX_WATER_OUTPUT_STEP "water_output_step"
#define WATER_INFO_EX_WATER_OUTPUT_DEFAULT "water_output_default"
#define WATER_INFO_EX_WATER_TEMP_MIN "water_temp_min"
#define WATER_INFO_EX_WATER_TEMP_MAX "water_temp_max"
#define WATER_INFO_EX_WATER_TEMP_STEP "water_temp_step"
#define WATER_INFO_EX_WATER_TEMP_DEFAULT "water_temp_default"

namespace water {


static std::list<SModeInfoEx> _self_modes;
static JsonHandler g_handler;

static Json::Value SParamToJson(const SModeInfoEx& s)
{
	Json::Value json;
	json[WATER_INFO_EX_MODE] = s.option.mode;
	json[WATER_INFO_EX_NAME] = s.name;
	json[WATER_INFO_EX_ICON] = s.icon;
	json[WATER_INFO_EX_COLLECTED_MODE] = s.collected_mode;
	json[WATER_INFO_EX_WATER_OUTPUT_MIN] = s.option.water_output.min;
    json[WATER_INFO_EX_WATER_OUTPUT_MAX] = s.option.water_output.max;
	json[WATER_INFO_EX_WATER_OUTPUT_STEP] = s.option.water_output.step;
	json[WATER_INFO_EX_WATER_OUTPUT_DEFAULT] = s.option.water_output.def;
	json[WATER_INFO_EX_WATER_TEMP_MIN] = s.option.water_temp.min;
	json[WATER_INFO_EX_WATER_TEMP_MAX] = s.option.water_temp.max;
	json[WATER_INFO_EX_WATER_TEMP_STEP] = s.option.water_temp.step;
	json[WATER_INFO_EX_WATER_TEMP_DEFAULT] = s.option.water_temp.def;
	return json;
}

static bool JsonToSParam(const Json::Value& json, SModeInfoEx& s)
{
	if(true == json.isObject()){
		int mode = 0;
		if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_MODE, mode)){
			LOGD("--%d-- --%s-- 解析mode失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
		s.option.mode = (EWaterMode)mode;

		if(false == JsonHandler::getObjString(json, WATER_INFO_EX_NAME, s.name)){
			LOGD("--%d-- --%s-- 解析name失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
		if(false == JsonHandler::getObjString(json, WATER_INFO_EX_ICON, s.icon)){
			LOGD("--%d-- --%s-- 解析icon失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
		int collected_mode = 0;
		if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_COLLECTED_MODE, collected_mode)){
			LOGD("--%d-- --%s-- 解析collected_mode失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
		s.collected_mode = (EWaterMode)collected_mode;
//		LOGD("s.option.mode: %d, collected_mode: %d", s.collected_mode, collected_mode);
		if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_WATER_OUTPUT_MIN, s.option.water_output.min)){
			LOGD("--%d-- --%s-- 解析water_output_min失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
		if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_WATER_OUTPUT_MAX, s.option.water_output.max)){
			LOGD("--%d-- --%s-- 解析work_time失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
        if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_WATER_OUTPUT_STEP, s.option.water_output.step)){
			LOGD("--%d-- --%s-- 解析water_output_step失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
        if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_WATER_OUTPUT_DEFAULT, s.option.water_output.def)){
			LOGD("--%d-- --%s-- 解析water_output_default失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
        if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_WATER_TEMP_MIN, s.option.water_temp.min)){
			LOGD("--%d-- --%s-- 解析water_temp_min失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
        if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_WATER_TEMP_MAX, s.option.water_temp.max)){
			LOGD("--%d-- --%s-- 解析water_temp_max失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
        if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_WATER_TEMP_STEP, s.option.water_temp.step)){
			LOGD("--%d-- --%s-- 解析water_temp_step失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
        if(false == JsonHandler::getObjInt(json, WATER_INFO_EX_WATER_TEMP_DEFAULT, s.option.water_temp.def)){
			LOGD("--%d-- --%s-- 解析water_temp_default失败！！\n%s\n", __LINE__, __FILE__, json.toStyledString().c_str());
			return false;
		}
	}else{
		LOGD("--%d-- --%s-- %s不是对象！！\n", __LINE__, __FILE__, json.toStyledString().c_str());
		return false;
	}
	return true;
}

void save_self_mode_item()
{
    Json::Value json;
    std::list<SModeInfoEx>::iterator it = _self_modes.begin();
    for(uint32_t i = 0; it != _self_modes.end(); it++, i++){
        json.append(SParamToJson(*it));
    }
    g_handler.saveToFile(SELF_WATER_MODE_JSON_PATH, json);
}

bool load_self_mode_item()
{
	_self_modes.clear();
    g_handler.loadFromFile(SELF_WATER_MODE_JSON_PATH);
    Json::Value json = g_handler.getRoot();
    if(!json.isArray()){
        LOGD("--%d-- --%s-- %s不是数组！！\n", __LINE__, __FILE__, json.toStyledString().c_str());
        return false;
    }
    for(uint32_t i = 0; i < json.size(); i++){
        SModeInfoEx s;
        if(false == JsonToSParam(json[i], s)){
            LOGD("--%d-- --%s-- 解析onekey_cook_item失败！！\n%s\n", __LINE__, __FILE__, json[i].toStyledString().c_str());
            return false;
        }
        _self_modes.push_back(s);
    }
    return true;
}

void add_self_mode(const SModeInfoEx &mode_info)
{
    _self_modes.push_back(mode_info);
    save_self_mode_item();
}

void remove_self_mode(int del_index)
{
	auto it = _self_modes.begin();

	// 使用 std::advance 移动迭代器到指定位置
	std::advance(it, del_index);

	if(it->collected_mode != E_WATER_MODE_NONE){
		for(auto it2 = _self_modes.begin(); it2 != _self_modes.end(); it2++){
			if(it2->option.mode != E_WATER_MODE_SELF){
				if(it2->collected_mode == it->collected_mode){
					it2->collected_mode = E_WATER_MODE_NONE;
				}
			}
		}
	}
	// 删除指定位置的元素
	_self_modes.erase(it);
	save_self_mode_item();
}

void clear_self_mode()
{
	_self_modes.clear();
	save_self_mode_item();
	init_self_mode_item();
}

void reset_self_mode(int index, const SModeInfoEx &mode_info)
{
	if(mode_info.option.mode != E_WATER_MODE_SELF){
		std::list<SModeInfoEx>::iterator it = _self_modes.begin();
		std::advance(it, index);
		*it = mode_info;
	}

    save_self_mode_item();
}

const SModeInfoEx& get_self_mode(int index)
{
    std::list<SModeInfoEx>::iterator it = _self_modes.begin();
    std::advance(it, index);
    return *it;
}

const SModeInfoEx& get_self_mode_by_mode(int mode)
{
	for(auto it = _self_modes.begin(); it != _self_modes.end(); it++){
		if(it->option.mode == mode){
			return *it;
		}
	}
	return _self_modes.front();
}

int get_self_mode_count()
{
    return _self_modes.size();
}

void init_self_mode_item()
{
//	if(!load_self_mode_item()){
//		_self_modes.clear();
//	}
//	if(_self_modes.empty()) {
		_self_modes.push_back({ "冰块", "Main/Main_Ice.png", E_WATER_MODE_NONE, { E_WATER_MODE_ICE, { 2, 31, 10, 1}, {-10,0,0,1} }});
		_self_modes.push_back({ "冰水", "Main/Main_IceWater.png", E_WATER_MODE_NONE, { E_WATER_MODE_ICE_WATER, { 150, 1050, 350, 50}, {0,0,10,0} }});
		_self_modes.push_back({ "常温水", "Main/Main_RoomTemp.png", E_WATER_MODE_NONE,  { E_WATER_MODE_COMMON_WATER, { 150, 1050, 350, 50},{-1,-1,25,-1}}});
		_self_modes.push_back({ "红茶", "Main/Main_BlackTea.png", E_WATER_MODE_NONE,  { E_WATER_MODE_RED_TEA, { 150, 1050, 350, 50}, { 45,100,100,5 } }});
		_self_modes.push_back({ "泡奶", "Main/Main_MilkFormula.png", E_WATER_MODE_NONE,  { E_WATER_MODE_BUBBLE_MILK, { 150, 1050, 350, 50}, { 45,100,45,5 } }});
		_self_modes.push_back({ "蜂蜜水", "Main/Main_Honey.png", E_WATER_MODE_NONE,  { E_WATER_MODE_HONEY_WATER, { 150, 1050, 350, 50}, { 45,100,60,5 } }});
		_self_modes.push_back({ "绿茶", "Main/Main_GreenTea.png", E_WATER_MODE_NONE,  { E_WATER_MODE_GREEN_TEA, { 150, 1050, 350, 50}, { 45,100,70,5 } }});
		_self_modes.push_back({ "花茶", "Main/Main_FlowerTea.png", E_WATER_MODE_NONE,  { E_WATER_MODE_FLAVOUR_TEA, { 150, 1050, 350, 50}, { 45,100,80,5 } }});
		_self_modes.push_back({ "普洱茶", "Main/Main_PuEr.png", E_WATER_MODE_NONE,  { E_WATER_MODE_PU_ERH_TEA, { 150, 1050, 350, 50}, { 45,100,90,5 } }});
		save_self_mode_item();
//	}
}

bool exchange_self_mode_item(int index1, int index2)
{
	if(index1 < 0 || index1 >= (int)_self_modes.size() || index2 < 0 || index2 >= (int)_self_modes.size()){
		return false;
	}
	auto it1 = _self_modes.begin();
	std::advance(it1, index1);
	auto it2 = _self_modes.begin();
	std::advance(it2, index2);
	std::swap(*it1, *it2);
	save_self_mode_item();
	return true;
}

bool del_self_mode_collection_item(int index) {
	auto it = _self_modes.begin();

	// 使用 std::advance 移动迭代器到指定位置
	std::advance(it, index);
	if(it->option.mode == E_WATER_MODE_SELF || it->collected_mode == E_WATER_MODE_NONE) {
		LOGD("del_self_mode_collection_item: %d, mode: %d, collected_mode: %d, is not a collection item\n", index, it->option.mode, it->collected_mode);
		return false;
	}
	EWaterMode collected_mode = it->collected_mode;
	it->collected_mode = E_WATER_MODE_NONE;
	for(auto it2 = _self_modes.begin(); it2 != _self_modes.end(); it2++){
		if(it2->option.mode == E_WATER_MODE_SELF) {
			if(it2->collected_mode == collected_mode){
				int del_index = std::distance(_self_modes.begin(), it2);
				remove_self_mode(del_index);
				return true;
			}
		}
	}
	return false;
}

}
